﻿/*
Key and Mouse comm...
V1.1
TOPHY...

*/

class KandM {
	//-----------------------属性
	[Inspectable(defaultValue="")]
	public var MouseDown:Function;
	[Inspectable(defaultValue="")]
	public var MouseUp:Function;
	[Inspectable(defaultValue="")]
	public var MouseMove:Function;
	[Inspectable(defaultValue="")]
	public var MouseDoubleDown:Function;
	[Inspectable(defaultValue="")]
	public var KeyDown:Function;
	[Inspectable(defaultValue="")]
	public var KeyUp:Function;
	[Inspectable(defaultValue="300")]
	public var MDtime:Number;
	//双击限时
	[Inspectable(defaultValue="200")]
	public var KLtime:Number;
	//粘连键限时
	//------------ mouse 标志
	public var MDC:Boolean = new Boolean();
	//左双击
	public var MSC:Boolean = new Boolean();
	//左单击
	public var MU:Boolean = new Boolean();
	//鼠标放开
	public var MD:Boolean = new Boolean();
	//鼠标按下
	public var x:Number = new Number();
	//鼠标x位置
	public var y:Number = new Number();
	//鼠标y位置
	//------------ key 标志
	public var KD:Boolean = new Boolean();
	//键盘按下
	public var KU:Boolean = new Boolean();
	//键盘放开
	public var K:Number = new Number();
	//最后一次按键的code
	//----------------------------------  标志性开关
	private var _ifwork:Boolean = new Boolean();
	private var enable:Boolean = new Boolean();
	private var LKck:Number=new Number();  //连键检查(防一直按着出粘连键)
	//------------------------- 参数
	private var KeventA:Array;
	//处理粘连键的函数数组
	private var KLbuf:Array;
	//粘连键
	private var MObj:Object;
	//监听器
	private var KObj:Object;
	//监听器
	private var Mt:Number;
	//上次按下mouse的时间
	private var Kt:Number;
	//上次按下key的时间
	//-------------------------  me
	private var _me:String = "KeyMouse";
	//模块名
	private var ___V:String = "";
	//版本及说明
	//-------------- 构造函数
	public function KandM(_MDtime:Number, _KLtime:Number) {
		if (_MDtime == undefined && MDtime ==undefined) {
			MDtime = 300;
		} else {
			MDtime = _MDtime;
		}
		if (_KLtime == undefined && KLtime ==undefined) {
			KLtime = 200;
		} else {
			KLtime = _KLtime;
		}
		//  调用初始化函数.
		init();
	}
	//--------------------------初始化
	private function init() {
		// -这里写初始化函数或内容代码。
		Mt = 0;
		Kt = 0;
		LKck=0;
		MObj = new Object();
		KObj = new Object();
		_ifwork = false;
		enable = true;
		MDC = false;
		MSC = false;
		MU = false;
		MD = false;
		KD = false;
		KU = false;
		K = 0;
		KeventA = new Array();
		KLbuf = new Array();
		//-------------------------------
		___V+="**** KeyMouse 组件V1.1 *******\r";
		___V+="copyright by TOPHY\r";
		___V+="2004/12/10\r";
		___V+="QQ:40730122\r";
		___V+="\r";
		___V+="------- 说明 ----------\r";
		___V+="实例.ifwork=true/false  起动或停止监听系统\r";
		___V+="实例.Enable=true/flase  暂时激活或停止动作\r";
		___V+="实例.MDC 布尔值获取当前鼠标是否双击状态\r";
		___V+="实例.MSC 布尔值获取当前鼠标是否单击状态\r";
		___V+="实例.MU  布尔值获取当前鼠标是否放开状态\r";
		___V+="实例.MD  布尔值获取当前鼠标是否按下状态\r";
		___V+="实例.MU  布尔值获取当前鼠标是否放开状态\r";
		___V+="实例.KD  布尔值获取当前按键是否按下状态\r";
		___V+="实例.KU  布尔值获取当前按键是否放开状态\r";
		___V+="实例.K  获取最后一次按键的ASCII码\r";
		___V+="实例.MouseDown=function 鼠标按下时触发的函数\r";
		___V+="实例.MouseUp=function 鼠标放开时触发的函数\r";
		___V+="实例.MouseDoubleDown=function 鼠标双击时触发的函数\r";
		___V+="实例.MouseMove=function 鼠标移动时触发的函数\r";
		___V+="实例.KeyDown=function 键盘按下时触发的函数\r";
		___V+="实例.KeyUp=function 键盘放开时触发的函数\r";
		___V+="实例.KLtime=Number 粘连键间隙时间\r";
		___V+="实例.MDtime=Number 鼠标双击间隙时间\r";
		___V+="\r";
		___V+=". . . . . . . . . . . . \r";
		___V+="实例.addNLK(Array,Fun) 加入粘连键码序及对应触发函数,示例入下：\r";
		___V+="var keys:Array=new Array(102,100,102,106); //组合键定义\r//或者这样：var keys:Array=new Array(\"f\",\"d\",\"f\",\"j\"); \r";
		___V+="var keyf:Function=function(){     //函数定义\r";
		___V+="    trace(\"触发了 f+d+f+k 的组合键\");\r";
		___V+="}\r";
		___V+="var NLKid:Number=实例.addNLK(keys,keyf);\r";
		___V+="\r";
		___V+="以上代码运行后，连续键入f d f  k 即可触发函数keyf\r";
		___V+="\r";
		___V+="实例.delNLK(id);  删除指定的粘连键定义\r";
		___V+="实例.delAllNLK();  删除所有粘连键定义\r";
		___V+="\r";
		___V+=". . . . . . . . . . . . \r";
		___V+="\r";
		___V+="-----------------------\r";
		___V+="\r";
		//-------------------------------
		trace(___V);
		//版本消息.
		say("初始化完成");
	}
	//------------------------ 消息函数
	private function say(msg:String) {
		trace(_me+":"+msg);
	}
	//=================================================扩展功能函数
	private function MDfun(tag:Object) {
		//鼠标按下
		//trace("鼠标按下"+tag.MouseDown);
		var dt:Date = new Date();
		var nowMt:Number = dt.getTime();
		var Lt:Number = nowMt-Mt;
		if (Lt<=MDtime) {
			tag.MDC = true;
			tag.MSC = false;
			tag.MDCfun(tag);
		} else {
			tag.MSC = true;
		}
		tag.Mt = nowMt;
		var f:Function = eval(tag.MouseDown);
		if (f != undefined) {
			if (typeof (f) == "function") {
				f();
			} else {
				tag.say("MouseDown不是有效的函数");
			}
		}
	}
	private function MUfun(tag:Object) {
		//鼠标放开
		//trace("鼠标放开");
		tag.MDC = false;
		var f:Function = eval(tag.MouseUp);
		if (f != undefined) {
			if (typeof (f) == "function") {
				f();
			} else {
				tag.say("MouseUp不是有效的函数");
			}
		}
	}
	private function MMfun(tag:Object) {
		//鼠标移动
		//trace("鼠标移动");
		var f:Function = eval(tag.MouseMove);
		if (f != undefined) {
			//trace(typeof(f)+" "+typeof(eval(tag.MouseMove)));
			if (typeof (f) == "function") {
				f();
			} else {
				tag.say("MouseMove不是有效的函数");
			}
		}
	}
	private function MDCfun(tag:Object) {
		//鼠标双击
		//trace("鼠标双击");
		var f:Function = eval(tag.MouseDoubleDown);
		if (f != undefined) {
			if (typeof (f) == "function") {
				f();
			} else {
				tag.say("MouseDoubleDown不是有效的函数");
			}
		}
	}
	private function KUfun(tag:Object) {
		//keyup
		//trace("keyup");
		tag.LKck=0;
		var f:Function = eval(tag.KeyUp);
		if (f != undefined) {
			if (typeof (f) == "function") {
				f();
			} else {
				tag.say("tag.KeyUp不是有效的函数");
			}
		}
	}
	private function KDfun(tag:Object) {
		//keydown
		//trace("keydown");
		//trace(tag.K);
		
		var nowDa:Date = new Date();
		var l = nowDa.getTime()-tag.Kt;
		tag.K = Key.getAscii();
		if (l<=tag.KLtime) {
			if(tag.K!=tag.LKck){
			tag.KLbuf.push(tag.K);
			}
		} else {
			tag.KLbuf = new Array();
			tag.KLbuf.push(tag.K);
		}
		if (tag.KeventA.length>0 && tag.KLbuf.length>1) {
			//处理粘连键
			tag.KLfun(tag);
		}
		tag.Kt = nowDa.getTime();
		var f:Function = eval(tag.KeyDown);
		if (f != undefined) {
			if (typeof (f) == "function") {
				f();
			} else {
				tag.say("KeyDown不是有效的函数");
			}
		}
		tag.LKck=Key.getAscii();
	}
	private function KLfun(tag:Object) {
		//粘连键动作
		//trace("粘连键");
		var taga = tag.KeventA;
		var don:Boolean = false;
		var lk:String = "";
		for (var i in taga) {
			if (taga[i].key.length == tag.KLbuf.length && taga[i]!=null) {
				var ii = 0;
				var notMh:Boolean = false;
				//找到未匹配
				lk = "";
				while (ii<taga[i].key.length) {
					if (taga[i].key[ii] != tag.KLbuf[ii]) {
						notMh = true;
						break;
					} else {
						lk += chr(tag.KLbuf[ii]);
					}
					ii++;
				}
				if (!notMh) {
					var f:Function = function () {
					};
					if (typeof (taga[i].fun) == "function") {
						f = taga[i].fun;
					} else {
						f = eval(taga[i].fun);
					}
					if (typeof (f) == "function") {
						f();
						don = true;
					} else {
						tag.say("粘连键["+lk+"]函数定义有误！");
					}
				}
			}
			if (don) {
				break;
			}
		}
		if (don) {
			tag.say("成功完成粘连键["+lk+"]");
		}
	}
	//===============================================系统功能函数
	public function delAllNLK(){
		if(enable)
		{
			KeventA=new Array();
			say("已删除所有粘连键定义");
		}else
		{
			say("目前不允许操作。");
		}
	};
	public function delNLK(id:Number){
		if(id==undefined || KeventA.length==0)
		{
			say("要删除的id不正确或目前无连键定义。");
			return ;
		}else
		{
			if (!_ifwork) {
			say("请先起动KeyMouse系统.");
			return;
			}
			if(id<KeventA.length){
				delete KeventA[id];
				say("已经删除["+id+"]号粘连键定义。");
			}
		}
	};
	public function addNLK(key:Array, fun:Function):Number {
		var reN:Number=-1;
		if (!_ifwork) {
			say("请先起动KeyMouse系统.");
			return -1;
		}
		if (key == undefined || fun == undefined) {
			say("加入粘连键的参数不完整。");
			return -1;
		} else {
			if (enable) {
				var have:Boolean = false;
				for (var i in KeventA) {
					//重复判断。
				}
				if (!have) {
					var temp:Array=new Array();
					if(typeof(key[0])!="number")
					{
						var i:Number=0;
						while(i<key.length)
						{
							temp.push(ord(key[i]));
							i++;
						}
						reN=KeventA.push({key:temp, fun:fun});
						reN--;
					}else{
						reN=KeventA.push({key:key, fun:fun});
						reN--;
					}
					say("加入粘连键成功。");
				}else
				{
					say("已经定义过这组粘连键了。");
					reN=-1;
				}
			}else
			{
				say("目前不允许加入粘连键定义。");
				reN=-1;
			}
			return reN;
		}
	}
	public function get Enable():Boolean {
		return enable;
	}
	public function set Enable(b:Boolean) {
		if (b) {
			enable = true;
		} else {
			enable = false;
		}
	}
	public function get ifwork():Boolean {
		return _ifwork;
	}
	public function set ifwork(w:Boolean) {
		if (w == undefined) {
			return;
		}
		if (w) {
			startMe();
		} else {
			stopMe();
		}
	}
	private function startMe() {
		//起动监听
		var me:Object = this;
		if (!_ifwork) {
			//--------------------------- mouse
			MObj.onMouseDown = function() {
				if (me.enable) {
					me.MDfun(me);
				}
			};
			MObj.onMouseUp = function() {
				if (me.enable) {
					me.MUfun(me);
				}
			};
			MObj.onMouseMove = function() {
				if (me.enable) {
					me.MMfun(me);
				}
			};
			Mouse.addListener(MObj);
			//--------------------------  key
			KObj.onKeyDown = function() {
				if (me.enable) {
					me.KDfun(me);
				}
			};
			KObj.onKeyUp = function() {
				if (me.enable) {
					me.KUfun(me);
				}
			};
			Key.addListener(KObj);
			_ifwork = true;
			trace("起动监听");
		}
	}
	private function stopMe() {
		//停止监听
		if (_ifwork) {
			Mouse.removeListener(MObj);
			Key.removeListener(KObj);
			_ifwork = false;
			trace("停止监听");
		}
	}
}
